{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Python异常处理\n",
    "\n",
    "### 1、 定位与调试\n",
    "第几行？\n",
    "\n",
    "什么错？\n",
    "\n",
    "怎么改？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (<ipython-input-1-1d2f1bbcc104>, line 4)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-1-1d2f1bbcc104>\"\u001b[1;36m, line \u001b[1;32m4\u001b[0m\n\u001b[1;33m    print (price is greater than 0)\u001b[0m\n\u001b[1;37m                               ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "price = 1\n",
    "if price>0:\n",
    "        print ('True')\n",
    "        print (price is greater than 0)\n",
    "else:\n",
    "    print \"False\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "以上报错代码显示第四行出现语法错误，是缩进错误。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "Missing parentheses in call to 'print' (<ipython-input-2-cf69990590e3>, line 6)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-2-cf69990590e3>\"\u001b[1;36m, line \u001b[1;32m6\u001b[0m\n\u001b[1;33m    print \"False\"\u001b[0m\n\u001b[1;37m                ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m Missing parentheses in call to 'print'\n"
     ]
    }
   ],
   "source": [
    "price=1\n",
    "if price>0:\n",
    "        print ('True')\n",
    "        print ('price is greater than 0')\n",
    "else:\n",
    "    print \"False\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "以上报错代码显示第六行出现语法错误，是少了括号。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "price is greater than 0\n"
     ]
    }
   ],
   "source": [
    "price=1\n",
    "if price>0:\n",
    "        print ('True')\n",
    "        print ('price is greater than 0')\n",
    "else:\n",
    "    print (\"False\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "以上是正确修改后的代码，无需所有缩进都对齐，只需同级对齐。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2、 try/except\n",
    "\n",
    "捕捉异常可以使用try/except语句。\n",
    "\n",
    "try/except语句用来检测try语句块中的错误，从而让except语句捕获异常信息并处理。\n",
    "\n",
    "如果你不想在异常发生时结束你的程序，只需在try里捕获它。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "try:\n",
    "\n",
    "    正常的操作\n",
    "    ......................\n",
    "except:\n",
    "\n",
    "    发生异常，执行这块代码\n",
    "    ......................\n",
    "else:\n",
    "\n",
    "    如果没有异常执行这块代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "invalid literal for int() with base 10: 'z66'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-6-3de6ec1555dc>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      3\u001b[0m     \u001b[1;32mreturn\u001b[0m \u001b[0mint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mprice\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      4\u001b[0m \u001b[1;31m# 调用函数\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 5\u001b[1;33m \u001b[0mtest_price\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"z66\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;32m<ipython-input-6-3de6ec1555dc>\u001b[0m in \u001b[0;36mtest_price\u001b[1;34m(price)\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;31m# 定义函数\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mtest_price\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mprice\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m     \u001b[1;32mreturn\u001b[0m \u001b[0mint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mprice\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      4\u001b[0m \u001b[1;31m# 调用函数\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[0mtest_price\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"z66\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mValueError\u001b[0m: invalid literal for int() with base 10: 'z66'"
     ]
    }
   ],
   "source": [
    "# 定义函数\n",
    "def test_price(price):\n",
    "    return int(price)\n",
    "# 调用函数\n",
    "test_price(\"z66\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "参数不是数字\n",
      "Go on\n"
     ]
    }
   ],
   "source": [
    "# 定义函数\n",
    "def test_price(price):\n",
    "    try:\n",
    "        p = int(price)\n",
    "    except ValueError:\n",
    "        print (\"参数不是数字\")\n",
    "    else:\n",
    "        print(p+100)\n",
    "    finally:\n",
    "        print('Go on')\n",
    "\n",
    "# 调用函数\n",
    "test_price(\"z66\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3、 Raise"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('Invalid price!', -1)\n"
     ]
    }
   ],
   "source": [
    "def check_price(price):\n",
    "    if price < 0:\n",
    "        raise Exception(\"Invalid price!\", price)\n",
    "        # 触发异常后，后面的代码就不会再执行\n",
    "\n",
    "try:\n",
    "    check_price(-1)\n",
    "    print('right')\n",
    "    ##触发异常\n",
    "except Exception as e:\n",
    "    print (e)\n",
    "else:\n",
    "    print ('can_trade')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4、 常见报错类型\n",
    "\n",
    "SyntaxError\tPython | 语法错误\n",
    "\n",
    "TypeError\t| 对类型无效的操作\n",
    "\n",
    "NameError\t| 未声明/初始化对象 (没有属性)\n",
    "\n",
    "ValueError\t| 传入无效的参数\n",
    "\n",
    "ImportError\t| 导入模块/对象失败\n",
    "\n",
    "查看更多： http://www.runoob.com/python/python-exceptions.html"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
